<template>
  <div>
    <el-dialog
      v-bind="$attrs"
      @open="onOpen"
      @close="onClose"
      v-model="dialogVisible"
      title="Dialog Titile"
    >
      <el-form
        ref="elForm"
        :model="formData"
        :rules="rules"
        size="medium"
        label-width="100px"
      >
        <el-form-item label="当前文件夹" prop="folderName">
          <el-input
            v-model="formData.folderName"
            placeholder="请输入当前文件夹"
            readonly
            clearable
            :style="{ width: '70%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="文件名" prop="flileName">
          <el-input
            v-model="formData.flileName"
            placeholder="请输入文件名"
            clearable
            :style="{ width: '70%' }"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="上传" prop="field103" required>
          <el-upload
            ref="field103"
            :file-list="field103fileList"
            :action="field103Action"
            :before-upload="field103BeforeUpload"
            :on-success="handleSuccess"
          >
            <el-button size="small" type="primary" icon="el-icon-upload"
              >点击上传</el-button
            >
          </el-upload>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script>
export default {
  inheritAttrs: false,
  uploadFormVisible: false,
  components: {},
  props: [],
  data() {
    return {
      formData: {
        folderName: undefined,
        flileName: undefined,
        field103: null,
      },
      rules: {
        folderName: [],
        flileName: [
          {
            required: true,
            message: "请输入文件名",
            trigger: "blur",
          },
        ],
      },
      field103Action: "/minio/upload",
      field103fileList: [],
      dialogVisible: false, // 添加此行，用于控制对话框的显示
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    onOpen() {},
    onClose() {
      this.$refs["elForm"].resetFields();
    },
    close() {
      // this.$emit("update:visible", false);
      this.dialogVisible = false;
    },
    handelConfirm() {
      this.$refs["elForm"].validate((valid) => {
        if (!valid) return;
        this.close();
      });
    },
    field103BeforeUpload(file) {
      let isRightSize = file.size / 1024 / 1024 < 2;
      if (!isRightSize) {
        this.$message.error("文件大小超过 2MB");
      }
      return isRightSize;
    },

    showDialog(row) {
      this.formData.folderName = row.name;
      this.dialogVisible = true; // 显示对话框
    },
    // 上传文件成功
    handleSuccess(response, file, fileList) {
      const fileName = file.name; // 从上传成功的文件对象中获取文件名
      this.formData.flileName = fileName; // 将文件名赋值给formData.flileName
    },
  },
};
</script>
<style>
.el-upload__tip {
  line-height: 1.2;
}
</style>
